package org.bluweb.g4wicket.calendar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.bluweb.g4wicket.G4WicketPanel;

@SuppressWarnings("serial")
public class Calendar extends G4WicketPanel {

	public static enum Mode { WEEK, MONTH, AGENDA };
	
	private String domain;
	private Mode mode = Mode.MONTH;
	private final List<String> sources = new ArrayList<String>();
	private final Map<String,String> colors = new HashMap<String, String>();
	
	private boolean showTitle = true;
	private boolean showNavigation = true;
	private boolean showDate = true;
	private boolean showPrint = true;
	private boolean showTabs = true;
	private boolean showCalendars = true;
	private boolean showTimezone = true;
	
	private final Label iframe = new Label( "iframe" );
	
	public Calendar( String id, String domain ) {
		super( id );
		this.domain = domain;
		this.add( iframe );
	}

	public boolean isShowTitle() {
		return showTitle;
	}

	public void setShowTitle(boolean showTitle) {
		this.showTitle = showTitle;
	}

	public boolean isShowNavigation() {
		return showNavigation;
	}

	public void setShowNavigation(boolean showNavigation) {
		this.showNavigation = showNavigation;
	}

	public boolean isShowDate() {
		return showDate;
	}

	public void setShowDate(boolean showDate) {
		this.showDate = showDate;
	}

	public boolean isShowPrint() {
		return showPrint;
	}

	public void setShowPrint(boolean showPrint) {
		this.showPrint = showPrint;
	}

	public boolean isShowTabs() {
		return showTabs;
	}

	public void setShowTabs(boolean showTabs) {
		this.showTabs = showTabs;
	}

	public boolean isShowCalendars() {
		return showCalendars;
	}

	public void setShowCalendars(boolean showCalendars) {
		this.showCalendars = showCalendars;
	}

	public boolean isShowTimezone() {
		return showTimezone;
	}

	public void setShowTimezone(boolean showTimezone) {
		this.showTimezone = showTimezone;
	}

	public Mode getMode() {
		return mode;
	}

	public void setMode(Mode mode) {
		this.mode = mode;
	}

	public void addSource( String source, String color ) {
		this.sources.add( source );
		this.colors.put( source, color );
	}
	
	@Override
	protected void onBeforeRender() {
		
		StringBuilder builder = new StringBuilder( "http://www.google.com/calendar/hosted/" );
		builder.append( this.domain );
		builder.append( "/embed?ctz=Europe/Rome" );
		
		if( this.mode != Mode.MONTH ) {
			builder.append( "&mode=" ).append( this.mode.toString() );
		}
		if( !this.showTitle ) {
			builder.append( "&showTitle=0" );
		}
		if( !this.showNavigation ) {
			builder.append( "&showNav=0" );
		}
		if( !this.showDate ) {
			builder.append( "&showDate=0" );			
		}
		if( !this.showPrint ) {
			builder.append( "&showPrint=0" );
		}
		if( !this.showTabs ) {
			builder.append( "&showTabs=0" );
		}
		if( !this.showCalendars ) {
			builder.append( "&showCalendars=0" );
		}
		if( !this.showTimezone ) {
			builder.append( "&showTz=0" );
		}
		
		for( String source : this.sources ) {
			builder.append( "&src=" ).append( source );
			builder.append( "&color=%23" ).append( this.colors.get( source ) );
		}
		
		this.iframe.add( new SimpleAttributeModifier( "src", builder.toString() ) );
		
		super.onBeforeRender();
	}
}
